package handel

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"user-reg-temp/model"
)

func Add(c *gin.Context) {
	username := c.PostForm("username")
	title := c.PostForm("title")
	img := c.PostForm("img")
	if username == "" || title == "" || img == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "参数错误",
		})
		return
	}
	data := model.User{
		Username: username,
		Title:    title,
		Img:      img,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var user []model.User
	model.DB.Offset(offset).Limit(size).Find(&user)
	var total int64
	model.DB.Model(&model.User{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "展示成功",
		"data":  user,
		"total": total,
	})
}
